<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/7/14
 * Time: 13:05
 */

namespace app\common\model;


class News extends Base
{
    /**
     * 后台新闻首页列表自动化分页
     * @param array $data
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getNews($data = [])
    {
        $data['status'] = [
            'neq',config('code.status_delete')
        ];

        $order = ['id'=>'desc'];    //id降序排列

        $result = $this->where($data)
                       ->order($order)
                       ->paginate();
        return $result;
    }

    /**
     * 根据条件获取数据
     * @param array $param
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNewsByCondition($condition = [],$start = 0,$size = 2)
    {
        //这里加一个isset来判断条件里是否有status是为了给app端用的
        if(!isset($condition['status']))
        {
            //我们后台要显示所有的新闻,包括审核通过的和未通过的,而app端只显示审核通过的
            $condition['status'] = [
                'neq',config('code.status_delete')
            ];
        }


        $order = ['id'=>'desc'];    //id降序排列

        //从哪里开始   最后将这个抽离出来
        //$start = ($condition['page'] - 1) * $condition['size'];

        // limit $start $size
        $result = $this->where($condition)
                       ->field($this->_getListField())
                       ->limit($start,$size)
                       ->order($order)
                       ->select();
        echo $this->getLastSql();
        return $result;
    }

    /**
     * 根据条件获取数据条数总数
     * @param array $condition
     * @return int|string
     */
    public function getNewsCountByCondition($condition = [])
    {
        if(!isset($condition['status']))
        {
            $condition['status'] = [
                'neq',config('code.status_delete')
            ];
        }

        return $this->where($condition)->count();
        //echo $this->getLastSql(); SELECT COUNT(*) AS tp_count FROM `news` WHERE `status` <> -1 LIMIT 1
    }

    /**
     * 获取首页头图的数据
     * @param int $num  默认显示的条数
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getIndexHeadNormalNews($num = 4)
    {
       //数据显示的条件
       $condition = [
           'status' => 1,
           'is_head_figure'=>1
       ];

       $order = [
           'id'=>'desc'
       ];

       return $this->where($condition)
                   ->field($this->_getListField())
                   ->order($order)
                   ->limit($num)
                   ->select();
    }

    /**
     * 获取推荐位的数据
     * @param int $num  默认显示的条数
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPositionNormalNews($num = 20)
    {
        //数据显示的条件
        $condition = [
            'status' => 1,
            'is_position'=>1
        ];

        $order = [
            'id'=>'desc'
        ];

        return $this->where($condition)
                    ->field($this->_getListField())
                    ->order($order)
                    ->limit($num)
                    ->select();
    }

    /**
     * 通用化获取数据字段
     * @return array
     */
    private function _getListField()
    {
        return [
            'id',
            'catid',
            'image',
            'title',
            'read_count',
            'is_position',
            'status',
            'create_time',
            'update_time'
        ];
    }

    /**
     * 根据阅读数获取排行新闻数据
     * @param int $num  默认展示5调数据
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRankNormalNews($num=5)
    {
        $condition = [
            'status' => 1
        ];

        $order = [
            'read_count'=>'desc'
        ];

        return $this->where($condition)
            ->field($this->_getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }
}